package com.andnnl.event.source;

import com.andnnl.event.events.Event;
import com.andnnl.event.listener.EventListener;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by chenss on 2021/5/11.
 */
public class Button implements EventSource {
    /**
     * 监听事件列表
     */
    protected List<EventListener<? extends Event>> listeners = new LinkedList<>();

    @Override
    public void addEventListener(EventListener<? extends Event> listener) {
        listeners.add(listener);
    }

    @Override
    public void removeEventListener(EventListener<? extends Event> listener) {
        listeners.remove(listener);
    }

    @Override
    public void notifyListeners(Event event) {
        for (EventListener listener : listeners) {
            try {
                listener.handleEvent(event);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }
}
